package com.example.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.IdRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * HorizontalScrollView和ViewPager联动效果 上面为HorizontalScrollView,下面为ViewPager
 *
 * @author zj 2012-5-23 下午1:07:06
 */
public class MainActivity extends Activity {

    Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn4_2;
    EditText a1, b1, a3, a2, b2, b3, lsn_jie1, lsn_jie2, lsn_jiao1, lsn_jiao2;
    double pi = 3.14, r = 0.8;
    TextView result1, result2, result3, result4, result5, p6r1, p6r2, p6r3, p6r4;
    Toast tst;
    boolean tag = false;
    EditText r_a1, r_b1, y_a3, y_b3, y_b4, y_a4, r_b2, r_a2, page6_d1, page6_d2, page6_d3, page6_d4, page6_d5;

    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private RadioButton mRadioButton4;
    private RadioButton mRadioButton5;
    private RadioButton mRadioButton6;
    private ImageView mImageView;
    private float mCurrentCheckedRadioLeft;// 当前被选中的RadioButton距离左侧的距离
    private HorizontalScrollView mHorizontalScrollView;// 上面的水平滚动控件
    private ViewPager mViewPager; // 下方的可横向拖动的控件
    private ArrayList<View> mViews;// 用来存放下方滚动的layout(layout_1,layout_2,layout_3)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        iniController();
        iniListener();
        iniVariable();

        mRadioButton1.setChecked(true);
        mViewPager.setCurrentItem(1);
        mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();

    }

    private void iniVariable() {
        // TODO Auto-generated method stub
        mViews = new ArrayList<View>();
        mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_1, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_2, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_3, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_4, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_5, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_6, null));
        mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null));

        mViewPager.setAdapter(new MyPagerAdapter());// 设置ViewPager的适配器

        /*
         * View view1=getLayoutInflater().inflate(R.layout.layout_1, null); btn1
         * = (Button) view1.findViewById(R.id.do1); btn1.setOnClickListener(new
         * OnClickListener() {
         *
         * @Override public void onClick(View v) { if (v==btn1) { Toast tst =
         * Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
         * tst.show(); }
         *
         * } });
         */

    }

    /**
     * RadioGroup点击CheckedChanged监听
     */

    OnCheckedChangeListener listener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            AnimationSet _AnimationSet = new AnimationSet(true);
            TranslateAnimation _TranslateAnimation;

            Log.i("zj", "checkedid=" + checkedId);
            if (checkedId == R.id.btn1) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo1), 0f, 0f);
                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);
                /*
                 * LayoutParams _LayoutParams1 = new LayoutParams(100, 4);
                 * _LayoutParams1.setMargins(0, 0, 0, 0);
                 * _LayoutParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                 */
                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);// 开始上面蓝色横条图片的动画切换
                // mImageView.setLayoutParams(_LayoutParams1);
                mViewPager.setCurrentItem(1);// 让下方ViewPager跟随上面的HorizontalScrollView切换
            } else if (checkedId == R.id.btn2) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo2), 0f, 0f);

                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);

                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);

                mViewPager.setCurrentItem(2);
            } else if (checkedId == R.id.btn3) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo3), 0f, 0f);

                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);

                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);

                mViewPager.setCurrentItem(3);
            } else if (checkedId == R.id.btn4) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo4), 0f, 0f);

                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);

                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);
                mViewPager.setCurrentItem(4);
            } else if (checkedId == R.id.btn5) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo5), 0f, 0f);

                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);

                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);

                mViewPager.setCurrentItem(5);
            } else if (checkedId == R.id.btn6) {
                _TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft,
                        getResources().getDimension(R.dimen.rdo6), 0f, 0f);

                _AnimationSet.addAnimation(_TranslateAnimation);
                _AnimationSet.setFillBefore(false);
                _AnimationSet.setFillAfter(true);
                _AnimationSet.setDuration(100);

                // mImageView.bringToFront();
                mImageView.startAnimation(_AnimationSet);

                mViewPager.setCurrentItem(6);
            }

            mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();

            Log.i("zj", "getCurrentCheckedRadioLeft=" + getCurrentCheckedRadioLeft());
            Log.i("zj", "getDimension=" + getResources().getDimension(R.dimen.rdo2));

            // mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2),
            // 0);
        }

    };

    /**
     * 获得当前被选中的RadioButton距离左侧的距离
     */
    private float getCurrentCheckedRadioLeft() {
        // TODO Auto-generated method stub
        if (mRadioButton1.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));
            return getResources().getDimension(R.dimen.rdo1);
        } else if (mRadioButton2.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));
            return getResources().getDimension(R.dimen.rdo2);
        } else if (mRadioButton3.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));
            return getResources().getDimension(R.dimen.rdo3);
        } else if (mRadioButton4.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo4));
            return getResources().getDimension(R.dimen.rdo4);
        } else if (mRadioButton5.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));
            return getResources().getDimension(R.dimen.rdo5);
        } else if (mRadioButton6.isChecked()) {
            // Log.i("zj",
            // "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo5));
            return getResources().getDimension(R.dimen.rdo6);
        }
        return 0f;
    }

    private void iniListener() {
        // TODO Auto-generated method stub

        mRadioGroup.setOnCheckedChangeListener(listener);

        mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());

    }

    private void iniController() {
        // TODO Auto-generated method stub
        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mRadioButton1 = (RadioButton) findViewById(R.id.btn1);
        mRadioButton2 = (RadioButton) findViewById(R.id.btn2);
        mRadioButton3 = (RadioButton) findViewById(R.id.btn3);
        mRadioButton4 = (RadioButton) findViewById(R.id.btn4);
        mRadioButton5 = (RadioButton) findViewById(R.id.btn5);
        mRadioButton6 = (RadioButton) findViewById(R.id.btn6);

        mImageView = (ImageView) findViewById(R.id.img1);

        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);

        mViewPager = (ViewPager) findViewById(R.id.pager);
    }

    /**
     * ViewPager的适配器
     *
     * @author zj 2012-5-24 下午2:26:57
     */
    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public void destroyItem(View v, int position, Object obj) {
            // TODO Auto-generated method stub
            ((ViewPager) v).removeView(mViews.get(position));
        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mViews.size();
        }

        @Override
        public Object instantiateItem(View v, int position) {

            btn1 = (Button) findViewById(R.id.do1);
            btn2 = (Button) findViewById(R.id.do2);
            btn3 = (Button) findViewById(R.id.do3);
            btn4 = (Button) findViewById(R.id.pac_do);
            btn4_2 = (Button) findViewById(R.id.pac_do2);
            btn5 = (Button) findViewById(R.id.lsn_do);
            btn6 = (Button) findViewById(R.id.bb_do);
            btn7 = (Button) findViewById(R.id.jy);

            a1 = (EditText) mViews.get(1).findViewById(R.id.a1);
            b1 = (EditText) mViews.get(1).findViewById(R.id.b1);

            a2 = (EditText) mViews.get(2).findViewById(R.id.a2);
            b2 = (EditText) mViews.get(2).findViewById(R.id.b2);

            a3 = (EditText) mViews.get(3).findViewById(R.id.a3);
            b3 = (EditText) mViews.get(3).findViewById(R.id.b3);

            r_a1 = (EditText) findViewById(R.id.r_a1);
            r_b1 = (EditText) findViewById(R.id.r_b1);
            y_a3 = (EditText) findViewById(R.id.y_a3);
            y_b3 = (EditText) findViewById(R.id.y_b3);
            r_a2 = (EditText) findViewById(R.id.r_a2);
            r_b2 = (EditText) findViewById(R.id.r_b2);
            y_a4 = (EditText) findViewById(R.id.y_a4);
            y_b4 = (EditText) findViewById(R.id.y_b4);
            lsn_jie1 = (EditText) findViewById(R.id.jie1);
            lsn_jie2 = (EditText) findViewById(R.id.jie2);
            lsn_jiao1 = (EditText) findViewById(R.id.jiao1);
            lsn_jiao2 = (EditText) findViewById(R.id.jiao2);

            page6_d1 = (EditText) findViewById(R.id.bbqs);
            page6_d2 = (EditText) findViewById(R.id.bbss);
            page6_d3 = (EditText) findViewById(R.id.bbyd);
            page6_d4 = (EditText) findViewById(R.id.bbpac);
            page6_d5 = (EditText) findViewById(R.id.bbclsn);

            result1 = (TextView) findViewById(R.id.result1);
            result2 = (TextView) findViewById(R.id.result2);
            result3 = (TextView) findViewById(R.id.result3);
            result4 = (TextView) findViewById(R.id.result_pac);
            result5 = (TextView) findViewById(R.id.result_lsn);

            p6r1 = (TextView) findViewById(R.id.p6r1);
            p6r2 = (TextView) findViewById(R.id.p6r2);
            p6r3 = (TextView) findViewById(R.id.p6r3);
            p6r4 = (TextView) findViewById(R.id.p6r4);

            mViews.get(1).findViewById(R.id.do1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /*
                     * Toast.makeText(MainActivity.this,"Click ok",
                     * Toast.LENGTH_SHORT).show();
                     */

                    String number1 = a1.getText().toString();
                    String number2 = b1.getText().toString();
                    if (number1.equals("") | number2.equals("")) {

                        tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                        tst.show();
                        return;
                    }
                    double c = Double.parseDouble(number1) - Double.parseDouble(number2);
                    c = 3.14 * 1 * 1 * c;
                    tst = Toast.makeText(MainActivity.this, c + "", Toast.LENGTH_LONG);
                    tst.show();
                    result1.setText(c + "");

                }

                ;
            });

            mViews.get(2).findViewById(R.id.do2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String n1 = a2.getText().toString();
                    String n2 = b2.getText().toString();
                    if (n1.equals("") | n2.equals("")) {

                        tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                        tst.show();
                        return;
                    }
                    double t2 = Double.parseDouble(n1) - Double.parseDouble(n2);
                    t2 = 3.14 * 0.7 * 0.7 * t2;
                    tst = Toast.makeText(MainActivity.this, t2 + "", Toast.LENGTH_LONG);
                    tst.show();
                    result2.setText(t2 + "");

                }

                ;
            });
            mViews.get(3).findViewById(R.id.do3).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String n1 = a3.getText().toString();
                    String n2 = b3.getText().toString();
                    if (n1.equals("") | n2.equals("")) {

                        tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                        tst.show();
                        return;
                    }
                    int t2 = Integer.parseInt(n1) - Integer.parseInt(n2);

                    tst = Toast.makeText(MainActivity.this, t2 + "", Toast.LENGTH_LONG);
                    tst.show();
                    result3.setText(t2 + "");

                }

                ;
            });
            mViews.get(4).findViewById(R.id.pac_do).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String n1 = r_a1.getText().toString(); /* 接班溶液池a1 */
                    String n2 = r_b1.getText().toString();/* 接班溶液池b1 */
                    String n3 = y_a3.getText().toString();/* 接班原液池a3" */
                    String n4 = y_b3.getText().toString();/* 接班原液池b3 */
                    String n5 = r_a2.getText().toString();/* 交班溶液池a2 */
                    String n6 = r_b2.getText().toString();/* 交班溶液池b2 */
                    String n7 = y_a4.getText().toString();/* 交班原液池a4 */
                    String n8 = y_b4.getText().toString();/* 交班原液池b4 */

                    String[] tt = {n1, n2, n3, n4, n5, n6, n7, n8};
                    for (int i = 0; i < tt.length; i++) {
                        if (tt[i].equals("")) {

                            tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                            tst.show();
                            return;
                        }

                    }

                    double t1 = 0.162 * Math.abs(Double.parseDouble(n1) + Double.parseDouble(n2)
                            - Double.parseDouble(n5) - Double.parseDouble(n6));
                    double t2 = 12.25 * Math.abs(Double.parseDouble(n3) + Double.parseDouble(n4)
                            - Double.parseDouble(n7) - Double.parseDouble(n8));
                    double res = t1 + t2;
                    tst = Toast.makeText(MainActivity.this, res + "", Toast.LENGTH_LONG);
                    tst.show();
                    result4.setText(res + "");

                }

                ;
            });
            mViews.get(4).findViewById(R.id.pac_do2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String n1 = r_a1.getText().toString(); /* 接班溶液池a1 */
                    String n2 = r_b1.getText().toString();/* 接班溶液池b1 */
                    String n3 = y_a3.getText().toString();/* 接班原液池a3" */
                    String n4 = y_b3.getText().toString();/* 接班原液池b3 */
                    String n5 = r_a2.getText().toString();/* 交班溶液池a2 */
                    String n6 = r_b2.getText().toString();/* 交班溶液池b2 */
                    String n7 = y_a4.getText().toString();/* 交班原液池a4 */
                    String n8 = y_b4.getText().toString();/* 交班原液池b4 */

                    String[] tt = {n1, n2, n3, n4, n5, n6, n7, n8};
                    for (int i = 0; i < tt.length; i++) {
                        if (tt[i].equals("")) {

                            tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                            tst.show();
                            return;
                        }

                    }

                    double t1 = 0.162 * Math.abs(Double.parseDouble(n1) + Double.parseDouble(n2) - 0.5 - 0.5 + 1.14
                            + 1.14 - Double.parseDouble(n5) - Double.parseDouble(n6));
                    double t2 = 0;
                    double res = t1 + t2;
                    tst = Toast.makeText(MainActivity.this, res + "", Toast.LENGTH_LONG);
                    tst.show();
                    result4.setText(res + "");

                }

                ;
            });
            mViews.get(5).findViewById(R.id.lsn_do).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String n1 = lsn_jie1.getText().toString();
                    String n2 = lsn_jie2.getText().toString();
                    String n3 = lsn_jiao1.getText().toString();
                    String n4 = lsn_jiao2.getText().toString();
                    String[] tt = {n1, n2, n3, n4};
                    for (int i = 0; i < tt.length; i++) {
                        if (tt[i].equals("")) {

                            tst = Toast.makeText(MainActivity.this, "请输入数据", Toast.LENGTH_LONG);
                            tst.show();
                            return;
                        }

                    }
                    double t2 = Double.parseDouble(n1) + Double.parseDouble(n2) - Double.parseDouble(n3)
                            - Double.parseDouble(n4);
                    t2 = pi * r * r * Math.abs(t2);
                    tst = Toast.makeText(MainActivity.this, t2 + "", Toast.LENGTH_LONG);
                    tst.show();
                    result5.setText(t2 + "");

                }

                ;
            });
            RadioGroup radioGroup = (RadioGroup) mViews.get(6).findViewById(R.id.rg);

            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                    if (checkedId == R.id.rb2) // rb2设定为正确答案
                    {

                        tag = true;
                    } else if (checkedId == R.id.rb1) {
                        tag = false;
                    }
                    RadioButton rb = (RadioButton) findViewById(checkedId);

                    //Toast.makeText(MainActivity.this, rb.getTag().toString(), Toast.LENGTH_LONG).show();
                    Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_LONG).show();
                }
            });

            mViews.get(6).findViewById(R.id.jy).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("button", "ID=jy button pressed !");
                    String n1 = (page6_d1.getText().toString() != null) ? page6_d1.getText().toString() : ""; // "本班取水
                    // 1#"
                    String n2 = (page6_d2.getText().toString() != null) ? page6_d2.getText().toString() : ""; // "本班送水
                    // 2#"
                    String n3 = (page6_d3.getText().toString() != null) ? page6_d3.getText().toString() : ""; // "本班用电量
                    // 3#"
                    String n4 = (page6_d4.getText().toString() != null) ? page6_d4.getText().toString() : ""; // "本班PAC消耗量
                    // 4#"
                    String n5 = (page6_d5.getText().toString() != null) ? page6_d5.getText().toString() : ""; // "本班次氯酸钠消耗量
                    // 5#"

                    String[] tt = {n1, n2, n3, n4, n5};
                    boolean go = false;
                    for (int i = 0; i < tt.length; i++) {

                        if (tt[i].equals("")) {
                            Toast.makeText(MainActivity.this, "请输入完整的5个数值！！", Toast.LENGTH_LONG).show();
                            return;
                        }


                    }

//					Toast.makeText(MainActivity.this, "jy", Toast.LENGTH_LONG).show();

                    AlertDialog.Builder builder = new Builder(MainActivity.this);
                    View checkview = View.inflate(MainActivity.this, R.layout.check, null);
                    //builder.setTitle("校验数据结果");


                    ArrayList<listdata> list = new ArrayList<listdata>();
                    ArrayList<range> item = new ArrayList<range>();
                    listdata k1 = new listdata(1, "本班取水           1#", "", 0);
                    listdata k2 = new listdata(2, "本班送水 			 2#", "", 0);
                    listdata k3 = new listdata(3, "本班用电量	  	 3#", "", 0);
                    listdata k4 = new listdata(4, "本班PAC消耗量		 4#", "", 0);
                    listdata k5 = new listdata(5, "本班次氯酸钠消耗量 5#", "", 0);
                    list.add(k1);
                    list.add(k2);
                    list.add(k3);
                    list.add(k4);
                    list.add(k5);

                    double[] max, min = null;
                    double[] day1 = {10000, 12000, 3000, 0.06, 0.12};
                    double[] day2 = {16000, 17000, 5000, 0.16, 0.24};
                    double[] night1 = {17000, 14000, 6000, 0.12, 0.19};
                    double[] night2 = {29000, 26000, 9000, 0.32, 0.52};

                    if (tag) {
                        min = night1;
                        max = night2;
                    } else {
                        min = day1;
                        max = day2;
                    }

                    range l1 = new range(1, k1.getName(), min[0], max[0]);
                    range l2 = new range(2, k2.getName(), min[1], max[1]);
                    range l3 = new range(3, k3.getName(), min[2], max[2]);
                    range l4 = new range(4, k4.getName(), min[3], max[3]);
                    range l5 = new range(5, k5.getName(), min[4], max[4]);

                    item.add(l1);
                    item.add(l2);
                    item.add(l3);
                    item.add(l4);
                    item.add(l5);

				/*	builder.setItems(new String[] { jiaoyan(l1, k1, n1),  jiaoyan(l2, k2, n2),  jiaoyan(l3, k3, n3),  jiaoyan(l4, k4, n4),
							jiaoyan(l5, k5, n5) }, null);
					builder.setPositiveButton("确定", null);
					//builder.setNegativeButton("取消", null);//
					builder.show();
*/

                    k1 = jiaoyan(l1, k1, n1);
                    k2 = jiaoyan(l2, k2, n2);
                    k3 = jiaoyan(l3, k3, n3);
                    k4 = jiaoyan(l4, k4, n4);
                    k5 = jiaoyan(l5, k5, n5);

                    TextView column1_2 = (TextView) checkview.findViewById(R.id.column1_2);
                    TextView column1_3 = (TextView) checkview.findViewById(R.id.column1_3);


                    TextView column2_2 = (TextView) checkview.findViewById(R.id.column2_2);
                    TextView column2_3 = (TextView) checkview.findViewById(R.id.column2_3);


                    TextView column3_2 = (TextView) checkview.findViewById(R.id.column3_2);
                    TextView column3_3 = (TextView) checkview.findViewById(R.id.column3_3);


                    TextView column4_2 = (TextView) checkview.findViewById(R.id.column4_2);
                    TextView column4_3 = (TextView) checkview.findViewById(R.id.column4_3);


                    TextView column5_2 = (TextView) checkview.findViewById(R.id.column5_2);
                    TextView column5_3 = (TextView) checkview.findViewById(R.id.column5_3);


                    column1_2.setText(k1.getResult());
                    column1_3.setText(k1.getValue() + "");

                    column2_2.setText(k2.getResult());
                    column2_3.setText(k2.getValue() + "");

                    column3_2.setText(k3.getResult());
                    column3_3.setText(k3.getValue() + "");

                    column4_2.setText(k4.getResult());
                    column4_3.setText(k4.getValue() + "");

                    column5_2.setText(k5.getResult());
                    column5_3.setText(k5.getValue() + "");


                    builder.setTitle("校验结果").setView(checkview).setPositiveButton("确定", null);
                    AlertDialog alertDialog = builder.create();

                    alertDialog.show();


                }


            });


            mViews.get(6).findViewById(R.id.bb_do).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    String n1 = (page6_d1.getText().toString() != null) ? page6_d1.getText().toString() : ""; // "本班取水
                    // 1#"
                    String n2 = (page6_d2.getText().toString() != null) ? page6_d2.getText().toString() : ""; // "本班送水
                    // 2#"
                    String n3 = (page6_d3.getText().toString() != null) ? page6_d3.getText().toString() : ""; // "本班用电量
                    // 3#"
                    String n4 = (page6_d4.getText().toString() != null) ? page6_d4.getText().toString() : ""; // "本班PAC消耗量
                    // 4#"
                    String n5 = (page6_d5.getText().toString() != null) ? page6_d5.getText().toString() : ""; // "本班次氯酸钠消耗量

                    String[] tt = {n1, n2, n3, n4, n5};
                    boolean go = false;
                    for (int i = 0; i < tt.length; i++) {

                        if (tt[i].equals("")) {
                            Toast.makeText(MainActivity.this, "请输入完整的5个数值！！", Toast.LENGTH_LONG).show();
                            return;
                        }


                    }
                    // 5#"
                    /*
                     * String[] tt = { n1, n2, n3, n4, n5 }; boolean go = false;
                     * for (int i = 0; i < tt.length; i++) {
                     *
                     * if (tt[i].equals("")) { go = true; tst =
                     * Toast.makeText(MainActivity.this, "请输入数据",
                     * Toast.LENGTH_LONG); tst.show(); return;
                     *
                     * } } if (!go) { for (int i = 0; i < tt.length; i++) {
                     *
                     * if (Double.parseDouble(tt[0]) < min[0] |
                     * Double.parseDouble(tt[0]) > max[0]) { tst =
                     * Toast.makeText(MainActivity.this, "取水 数据异常！！",
                     * Toast.LENGTH_LONG); tst.show(); return; } else if
                     * (Double.parseDouble(tt[1]) < min[1] |
                     * Double.parseDouble(tt[1]) > max[1]) { tst =
                     * Toast.makeText(MainActivity.this, "送水 数据异常！！",
                     * Toast.LENGTH_LONG); tst.show(); return; } else if
                     * (Double.parseDouble(tt[2]) < min[2] |
                     * Double.parseDouble(tt[2]) > max[2]) { tst =
                     * Toast.makeText(MainActivity.this, "用电量 数据异常！！",
                     * Toast.LENGTH_LONG); tst.show(); return; } else if
                     * (Double.parseDouble(tt[3]) < min[3] |
                     * Double.parseDouble(tt[3]) > max[3]) { tst =
                     * Toast.makeText(MainActivity.this, "PAC 数据异常！",
                     * Toast.LENGTH_LONG); tst.show(); return; } else if
                     * (Double.parseDouble(tt[4]) < min[4] |
                     * Double.parseDouble(tt[4]) > max[4]) { tst =
                     * Toast.makeText(MainActivity.this, "次氯酸钠 数据异常！",
                     * Toast.LENGTH_LONG); tst.show(); return; }
                     *
                     * } }
                     */

                    double s1 = (Double.parseDouble(n1) - Double.parseDouble(n2)) / Double.parseDouble(n1) * 100;
                    p6r1.setText(s1 + " %");
                    double s2 = Double.parseDouble(n3) / Double.parseDouble(n2) * 1000;
                    p6r2.setText(s2 + "");
                    double s3 = Double.parseDouble(n4) * 1.4 / Double.parseDouble(n2) * 1000000;

                    p6r3.setText(s3 + "");
                    double s4 = Double.parseDouble(n5) * 1.2 / Double.parseDouble(n2) * 1000000;

                    p6r4.setText(s4 + "");

                }

                ;
            });

            ((ViewPager) v).addView(mViews.get(position));
            return mViews.get(position);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }

    private listdata jiaoyan(range item, listdata data, String db) {
        Double min = item.getMin();
        Double max = item.getMax();
        Double input = Double.parseDouble(db);
        data.setResult("no");
        if (input < min) {
            data.setValue(min - input);

        } else if (input > min && input < max) {
            data.setValue(((input - min) > (max - input)) ? (max - input) : (input - min));
            data.setResult("yes");
        } else if (input > max) {
            data.setValue(input - max);
        }
        return data;
    }

    /**
     * ViewPager的PageChangeListener(页面改变的监听器)
     *
     * @author zj 2012-5-24 下午3:14:27
     */
    private class MyPagerOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        /**
         * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
         */
        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            // Log.i("zj", "position="+position);

            if (position == 0) {
                mViewPager.setCurrentItem(1);
            } else if (position == 1) {
                mRadioButton1.performClick();
            } else if (position == 2) {
                mRadioButton2.performClick();
            } else if (position == 3) {
                mRadioButton3.performClick();
            } else if (position == 4) {
                mRadioButton4.performClick();
            } else if (position == 5) {
                mRadioButton5.performClick();
            } else if (position == 6) {
                mRadioButton6.performClick();

            } else if (position == 7) {
                mViewPager.setCurrentItem(6);
            }
        }

    }

}